%                    Airfoil Analysis
%
%
% Created by:  David Gitz (davidgitz@gmail.com
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Written for: AIAA Student Org
%
%             Do not use without explicit permission from
%             either the author or organization this program
%             was written for. Please cite the author for
%             any code/ideas/thought that was borrowed from
%             this work.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
whichFoil = 1;
whichRe   = 1;
clear myFoils;

reply = input('What is the lowest Reynolds Number of Interest (Drag Only)?');
lowRE = reply;
reply = input('What is the highest Reynolds Number of Interest (Drag Only)?');
highRE = reply;
reply = input('What is the lowest Average Moment Coefficient?');
lowMom = reply;
reply = input('What is the highest Average Moment Coefficient?');
highMom = reply;
skippeddatacounter = 0;
skippedairfoilcounter = 0;
noAddCurAirFoil = 0;
addCurRe = 1;
newFoilIndex = 1;
newReIndex = 1;
%myFoils = [airfoils(1)];
InterestTag = 0;
for whichFoil = 1:length(AirFoils)  %Loop through every airfoil for Drag Data
    InterestTag = 0;
    newReIndex = 1;
    %Loop through Drag Data
    if ( length(AirFoils(whichFoil).DragData) >= 1)  %Make sure Airfoil(x) cell contains data
        if(length(AirFoils(1).DragData(1).Cl)>0)  %Make sure Airfoil(x).data(y).Cl contains data
            for whichRe = 1:length(AirFoils(whichFoil).DragData)  %Loop through each Reynolds Number listed
                addCurRe = 1;  %Reset addCurRe tag
                if (AirFoils(whichFoil).DragData(whichRe).Re <= lowRE)  %Airfoil(x).data(y).Re is too low for the specified constraints
                    skippeddatacounter = skippeddatacounter + 1;
                    addCurRe = 0;
                elseif (AirFoils(whichFoil).DragData(whichRe).Re >= highRE)  %Airfoil(x).data(y).Re is too high for the specified constraints
                    skippeddatacounter = skippeddatacounter + 1;
                    addCurRe = 0;
                elseif (0) %Other checks for non-relevant Drag Data
                else  %Data is relevant
                    addCurRe = 1;
                    noAddCurAirFoil = 0;
                    InterestTag = InterestTag || 1;
                end
                if(addCurRe)  %Add this current Reynolds Number
                    myFoils(newFoilIndex).DragData(newReIndex) = AirFoils(whichFoil).DragData(whichRe);
                    newReIndex = newReIndex + 1;
                end
            end
        else  %Airfoil doesn't contain any data
            skippedairfoilcounter = skippedairfoilcounter + 1;
            noAddCurAirFoil = 1;
        end
    else
        noAddCurAirFoil = 1;
    end
    
    myFoils(newFoilIndex).name = AirFoils(whichFoil).name;
    myFoils(newFoilIndex).builder = AirFoils(whichFoil).builder;
    myFoils(newFoilIndex).comment = AirFoils(whichFoil).comment;
    myFoils(newFoilIndex).Interest = InterestTag;  %Tag for Foil of Interest
    newFoilIndex = newFoilIndex + 1;
end
newFoilIndex = 1;
newReIndex = 1;
for whichFoil = 1:length(AirFoils)  %Loop through every airfoil for Lift Data
    InterestTag = 0;
    if ( length(AirFoils(whichFoil).LiftData) >= 1)  %Make sure Airfoil(x) cell contains Lift Data
        
        for whichRe = 1:length(AirFoils(whichFoil).LiftData)
            addCurRe = 1;  %Reset addCurRe tag
%             if (AirFoils(whichFoil).LiftData(whichRe).Re <= lowRE)  %Airfoil(x).data(y).Re is too low for the specified constraints
%                 skippeddatacounter = skippeddatacounter + 1;
%                 addCurRe = 0;
%             elseif (AirFoils(whichFoil).LiftData(whichRe).Re >= highRE)  %Airfoil(x).data(y).Re is too high for the specified constraints
%                 skippeddatacounter = skippeddatacounter + 1;
%                 addCurRe = 0;
            if ((sum(AirFoils(1).LiftData(1).Cm)/length(AirFoils(1).LiftData(1).Cm)) <= lowMom)
                skippeddatacounter = skippeddatacounter + 1;
                addCurRe = 0;
            elseif ((sum(AirFoils(1).LiftData(1).Cm)/length(AirFoils(1).LiftData(1).Cm)) >= highMom)
                skippeddatacounter = skippeddatacounter + 1;
                addCurRe = 0;
            elseif (0) %Other checks for non-relevant Lift Data
            else  %Data is relevant
                addCurRe = 1;
                noAddCurAirFoil = 0;
                InterestTag = (InterestTag || 1);
            end
            if(addCurRe)  %Add this current Reynolds Number
                myFoils(newFoilIndex).LiftData(newReIndex) = AirFoils(whichFoil).LiftData(whichRe);
                newReIndex = newReIndex + 1;
            else
                InterestTag = 0;
            end
            a = myFoils(newFoilIndex).Interest && InterestTag;
            myFoils(newFoilIndex).Interest = myFoils(newFoilIndex).Interest && InterestTag;
        end
    end
    
    newFoilIndex = newFoilIndex + 1;
    newReIndex = 1;
end



%Remove any empty data
i = 1;
while(1)
    if (i > length(myFoils))
        break;
    elseif (~myFoils(i).Interest)
        myFoils(i) = [];
    else
        i = i+1;
    end
end